package com.blb.day15_restful.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.day15_restful.entity.Book;
import com.blb.day15_restful.entity.JsonResult;
import com.blb.day15_restful.service.BookService;
import com.blb.day15_restful.util.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/books")
    public JsonResult findAll(){
        try {
            List<Book> list = bookService.list();
            return new JsonResult(1,list);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @GetMapping("/books/page/{current}")
    public JsonResult findPage(@PathVariable("current")Integer current){
        try {
            //创建分页参数对象
            IPage<Book> pageArg = new Page<>();
            pageArg.setSize(10);            //每页长度
            pageArg.setCurrent(current);    //当前页数
            //分页查询结果
            IPage<Book> page = bookService.page(pageArg);
            return new JsonResult(1,page);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @GetMapping("/book/{id}")
    public JsonResult findById(@PathVariable("id")Integer id){
        try {
            Book book = bookService.getById(id);
            return new JsonResult(1,book);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PostMapping("/book")
    public JsonResult saveBook(@RequestBody Book book){
        try {
            bookService.save(book);
            return new JsonResult(1,null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PutMapping("/book")
    public JsonResult updateBook(@RequestBody Book book){
        try {
            bookService.updateById(book);
            return new JsonResult(1,null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @DeleteMapping("/book/{id}")
    public JsonResult delBook(@PathVariable("id")Integer id){
        try {
            bookService.removeById(id);
            return new JsonResult(1,null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PostMapping("/upload")
    public JsonResult uploadImage(@RequestParam("pic")MultipartFile image) throws IOException {
        try {
            String filename = image.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf("."));
            filename = UUID.randomUUID().toString().replace("-","") + suffix;
            //上传
            FtpUtil.upload("192.168.159.188",21,"ftpuser","123456",
                    "/home/ftpuser/www/images",filename,image.getInputStream());
            //返回图片地址
            return new JsonResult(1,"http://192.168.159.188/images/"+filename);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

}
